package com.example.springboot.controller;

import com.example.springboot.common.HoneyLogs;
import com.example.springboot.common.LogType;
import com.example.springboot.common.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.entity.User;
import com.example.springboot.service.IUserService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

import com.example.springboot.service.ICourseService;
import com.example.springboot.entity.Course;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wgc
 * @since 2023-08-03
 */
@RestController
@RequestMapping("/course")
public class CourseController {

    @Resource
    private ICourseService courseService;

    @Resource
    private IUserService userService;

    //新增和修改
    @PostMapping
    @HoneyLogs(operation = "课程",type = LogType.ADD)
    public Result save(@RequestBody Course course) {  //@RequestBody接收前端数据
        //新增或者更新
        return Result.success(courseService.saveOrUpdate(course));
    }

    @HoneyLogs(operation = "课程",type = LogType.UPDATE)
    @PostMapping("/update")
    public Result update(@RequestBody Course course) {  //@RequestBody接收前端数据
        //新增或者更新
        return Result.success(courseService.updateById(course));
    }

    @Transactional  //控制事务保留原属性
    @PostMapping("/studentCourse/{courseId}/{studentId}")
    public Result studentCourse(@PathVariable Integer courseId, @PathVariable Integer studentId) {  //@RequestBody接收前端数据
        //新增或者更新
        courseService.setStudentCourse(courseId,studentId);
        return Result.success();
    }

    //查询所有数据
    @GetMapping()
    public Result findAll() {
        return Result.success(courseService.list());
    }

    //删除
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        return Result.success(courseService.removeById(id));
    }

    //批量删除
    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {

        return Result.success(courseService.removeByIds(ids));
    }

    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(courseService.getById(id));
    }

    /**
     * @GetMapping("/page") public Result findPage(@RequestParam Integer pageNum,
     * @RequestParam Integer pageSize) {
     * //        TimeUnit.SECONDS.sleep(20); 设置响应时间
     * QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
     * queryWrapper.orderByDesc("id");
     * Page<Course> page = courseService.page(new Page<>(pageNum, pageSize), queryWrapper);
     * //      page.getRecords()  你可以获取当前页的记录列表
     * List<Course> records = page.getRecords();
     * for (Course record : records){
     * //通过Course类中的TeacherId查找user相同的id
     * User user = userService.getById(record.getTeacherId());
     * if(user != null){
     * record.setTeacher(user.getNickname());
     * }
     * }
     * return Result.success(page);
     * }
     */

    @GetMapping("/page")
    public Result findPage(@RequestParam String name,
                           @RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {

        Page<Course> page = courseService.findPage(new Page<>(pageNum, pageSize), name);
        return Result.success(page);
    }
}



